package com.szbmyoubao.app.utils

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.TextView
import com.ellecity06.common.utils.NumberUtils

/**
 * @author ellecity06
 * @time 2025/1/15 11:08
 * @des
 */
class AnimeUtils {
    companion object {
        fun alpha(view: View, duration: Long=100L, startAlpha: Float=0.0F, endAlpha: Float=1.0F): AnimatorSet {
            val objectAnimationAlpha = ObjectAnimator.ofFloat(view, "alpha", startAlpha, endAlpha)
            objectAnimationAlpha.duration = duration
            objectAnimationAlpha?.repeatMode = ValueAnimator.RESTART
            objectAnimationAlpha?.interpolator = LinearInterpolator()

            val animatorSet = AnimatorSet()
            return animatorSet.apply {
                play(objectAnimationAlpha).with(objectAnimationAlpha)
            }
        }

        /**
         * 文字变化动画
         */
        fun numberChangeAnimation(
            textView: TextView,
            startNumber: Float,
            endNumber: Float,
            duration: Long,
            digits: Int=0
        ): ValueAnimator {
            var currentNumber = startNumber
            val valueAnimator: ValueAnimator = ValueAnimator.ofFloat(startNumber, endNumber)
            valueAnimator.repeatMode = ValueAnimator.REVERSE

            valueAnimator.addUpdateListener {
                val number = it.animatedValue as Float
                if ((number - currentNumber) >= 0.05 || number >= endNumber) {
                    textView.text =  "${NumberUtils.format(number, digits)}"
                    currentNumber == number
                }


            }
            valueAnimator.duration = duration
            return valueAnimator
        }
    }
}